<cfcomponent displayname="ORM Service" hint="ORM Service" output="false">

<cffunction name="addIncludes">
  <cfinclude template="../includes.cfm" />
</cffunction>

<cffunction name="init">
	<cfargument name="hibernateConfigFile" required="true" />
	<cfargument name="packagename" required="true" />
	<cfscript>
		var incs = addIncludes();
		var params = structNew();
		var thisdir = GetDirectoryFromPath(getMetaData(this).path);
		var package = listFirst(arguments.packagename,".");
		var srcLocation = thisdir & "src/";
		var hbmsLocation = srcLocation;
		var packageSrcLoc = srcLocation & package & "/";
		var packageJar = srcLocation & package & ".jar";
	  hibernateFactory = includes.hibernateFactory.init(srcLocation);
	  hibernateTools = includes.hibernateTools.init();
	  hibernateTools.setHibernateFactory(hibernateFactory);
	  hibernateTools.setJarUtils(includes.jarUtils);
		beanInjector = includes.beanInjector.init(hibernateTools);
	  hibernateFactory.setBeanInjector(beanInjector);
	  hibernateService = includes.hibernateService.init(hibernateFactory,
		  hibernateConfigFile,
		  srcLocation,
		  srcLocation,
		  srcLocation,
		  expandpath("./"),
		  srcLocation
		  );
	  hibernateService.setHibernateTools(hibernateTools);

		if (NOT fileExists(packageJar)) {
			// have to load mappings into jar for some reason (classloader issue)
			hibernateTools.createJarFile(packageSrcLoc,packageJar);
			// create the java files (reading just compiled jar)
			hibernateTools.hbm2java(arguments.hibernateConfigFile,packageJar,srcLocation);
			// compile the files
			hibernateTools.compile(srcLocation,"",hbmsLocation);
			// new jar with java sources
			hibernateTools.createJarFile(packageSrcLoc,packageJar);
		}
			params["hibernate.dialect"] = "org.hibernate.dialect.HSQLDialect";
      params["hibernate.connection.driver_class"] = "org.hsqldb.jdbcDriver";
      params["hibernate.connection.url"] = "jdbc:hsqldb:mem:coldshendb";
      params["hibernate.connection.username"] = "sa";
      params["hibernate.connection.password"] = "";
      params["hibernate.connection.pool_size"] = "1";
      params["hibernate.connection.autocommit"] = "true";
      params["hibernate.cache.provider_class"] = "org.hibernate.cache.HashtableCacheProvider";
      params["hibernate.hbm2ddl.auto"] = "create-drop";
//      params["hibernate.default_schema"] = "coldshen";
      params["hibernate.show_sql"] = "true";

	 	variables.hibConfig = hibernateService.createConfiguration(params);
	 	
//	 	dumpvar(variables.hibConfig);
		variables.hibConfig = hibernatefactory.getConfiguration().configure(hibernateConfigFile);
		variables.hibernateFactory = hibernateFactory;
		variables.hibConfig.addHbmLocation(packageJar);
		variables.sessionFactory = variables.hibConfig.buildSessionFactory();

		return this;
	</cfscript>
</cffunction>

<cffunction name="getSessionFactory">
	<cfreturn variables.sessionFactory />
</cffunction>

<cffunction name="create">
	<cfargument name="classname" required="true" />
	<cfreturn variables.hibernatefactory.create(arguments.classname) />
</cffunction>

<cffunction name="load">
	<cfargument name="classname" required="true" />
	<cfargument name="recordId" required="true" />
	<cfscript>
		var record = "";
		var currentSession = "";
		try {
			record = create(arguments.classname).newInstance();
			if(val(arguments.recordId) neq 0) {
		 		currentSession = getSessionFactory().openSession();
	      currentSession.beginTransaction();
				currentSession.load( record, javacast("int",val(arguments.recordId)) );
	      currentSession.getTransaction().commit();
			}
		} 
		catch(Any excpt) {
			// dump of struct is endless loop do to cf introspection haywireness so just do msg/stack
		  dumpvar(excpt.message & " " & excpt.stacktrace);
		}
		return record;
	</cfscript>
</cffunction>


<cffunction name="query">
	<cfargument name="querystr" required="true" />
	<cfscript>
		var currentSession = "";
		var result = "";
			try {
		 		currentSession = getSessionFactory().openSession();			
				currentSession.beginTransaction();
				result = currentSession.createQuery(arguments.querystr).list();
				currentSession.getTransaction().commit();
			} 
			catch(Any excpt) {
				// dump of struct is endless loop do to cf introspection haywireness so just do msg/stack
			  dumpvar(excpt.message & " " & excpt.stacktrace);
			}
			return result;
	</cfscript>
	<cfreturn variables.hibernatefactory.create(arguments.classname) />
</cffunction>


<cffunction name="dumpvar">
  <cfargument name="var" default="blank">
  <cfargument name="abort" default="true">
  <cftry>
	  <cfdump var="#var#">
	<cfcatch>
	  <cfset writeoutput(cfcatch.Message & " " & cfcatch.Detail & " " & cfcatch.TagContext[1].line & " " & cfcatch.stacktrace) />
	</cfcatch>
	</cftry>
	<cfif arguments.abort>
	  <cfabort />
	</cfif>
</cffunction>


</cfcomponent>